package com.cskaoyan.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * 创建日期: 2022/08/31 11:30
 *
 * @author ciggar
 */
public class MybatisUtils {

    static SqlSession sqlSession;

    static {

        try {

            // 1. 读取配置文件
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");

            // 2. 获取SqlSessionFactory
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(in);

            // 3. 获取SqlSession
            sqlSession = sqlSessionFactory.openSession(true);

        }catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    // 获取SqlSession
    public static SqlSession getSqlSession() {
        return sqlSession;
    }
}
